package it.abc.sicsic.model.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Date;

//import org.apache.log4j.Logger;

public class Util {
    //private static final Logger logger = Logger.getLogger(Util.class);

    public Util() {
        super();
    }
    
    /**
     *Convert a Date in a String, by the given pattern
     * @param date The Date to convert, if null, returns null
     * @param pattern The String patter, Ex: yyyyMMdd
     * @return The converted String
     * @Author Alessandro Casolla
     */
    public static String convertDateToString(Date date, String pattern) {
        String sDate = "";
        try {
            if (date == null)
                return null;

            if (date != null) {
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
                sDate = simpleDateFormat.format(date);
            }

        } catch (Exception e) {
            //logger.error(e);
        }
        return sDate;
    }


    /**
     * Convert a String in a Date, by the given pattern of the String value, if Date is null, returns null
     * @param date A String rapresentation of a Date
     * @param pattern The String pattern, Ex : yyyyMMdd
     * @return The parsed Date, null if parse valid
     * @Author Alessandro Casolla
     */

    public static Date convertStringToDate(String date, String pattern) {

        if (date == null)
            return null;
        Date d = null;
        if (date != null) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);

            try {
                /*
                if (date.length() == 10)
                    d = simpleDateFormat.parse(Utils.formatColumnDateForSave(date));

                else if (date.length() == 8)
            */
                d = simpleDateFormat.parse(date);

            } catch (ParseException e) {
                //logger.error(e);
                return null;
            }
        }
        return d;
    }
}
